package com.softer.chengxin.blacklist.controller;

import com.softer.chengxin.blacklist.domin.BlacklistVO;
import com.softer.chengxin.blacklist.service.BlacklistService;
import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.domin.ManagerPageResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.ResultEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author wangixnrui
 * @date 2018/10/12
 */
@RestController
@RequestMapping(value = "api/Blacklist")
public class BlacklistController {

    @Autowired
    private BlacklistService blacklistService;

    /**
     * 主管部门查询企业黑名单列表
     * @param page 页码
     * @param qyName 企业名
     * @param cxDateBegin 查询录入时间开始
     * @param cxDateEnd 查询录入时间结束
     * @param state 状态
     * @param session sessin
     * @return 结果
     */
    @PostMapping(value = "/load")
    public ManagerPageResult load(Page page, String qyName, String cxDateBegin, String cxDateEnd, Integer state, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)){
            return new ManagerPageResult();
        }
        return blacklistService.load(page,qyName,cxDateBegin,cxDateEnd,state,loginName);
    }

    /**
     * 新增黑名单
     * @param blacklist 实体
     * @return 结果
     */
    @PostMapping(value = "/add")
    public CommonResult add(BlacklistVO blacklist,HttpSession session){
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)){
            return new CommonResult(ResultEnum.FAIL.getCode(), Common.ADD_FAIL);
        }
        return blacklistService.add(blacklist,loginName);
    }

    /**
     * 移除黑名单
     * @param blacklistVO 实体
     * @param session session
     * @return 结果
     */
    @PostMapping(value = "/remove")
    public CommonResult remove(BlacklistVO blacklistVO,HttpSession session){
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)){
            return new CommonResult(ResultEnum.FAIL.getCode(),Common.REMOVE_FAIL);
        }
        return blacklistService.remove(blacklistVO,loginName);
    }

    /**
     * 获取黑名单详情
     * @param id 黑名单id
     * @return 结果
     */
    @PostMapping(value = "/getBlackDetail")
    public BlacklistVO getBlackDetail(@RequestParam("id") Integer id){
        return blacklistService.getBlackDetail(id);
    }


}
